package scau.jump.securitytrace;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;

public class ConvertToVectorUtils {
	public static Vector<?> convertToVector(List<Object> objs) {
		if(objs == null || objs.size()<1) {
			return null;
		}
		
		Vector<Vector<?>> vector = new Vector<Vector<?>>();
		for(Object obj : objs) {
			vector.add(convertToVector(obj));
		}
		return vector;
	}

	public static Vector<?> convertToVector(Object obj) {
		if(obj == null) {
			return null;
		}
		Field[] fields = obj.getClass().getDeclaredFields();
		Vector<String> vector = new Vector<String>();
		for(int i = 0; i < fields.length; i ++ ) {
			String fieldName = fields[i].getName();
			String getMethodName = "get" + fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1);
			Method getMethod;
			try {
				getMethod = obj.getClass().getDeclaredMethod(getMethodName, new Class<?>[]{});
				String fieldValue = getMethod.invoke(obj, new Object[]{}).toString();
				vector.add(fieldValue);
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return vector;
	}
/*
	public static Vector<?> convertToVector(Object obj) {
		if(obj == null) {
			return null;
		}
		Vector<Object> vector = new Vector<Object>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] descriptions = beanInfo.getPropertyDescriptors();
			for(PropertyDescriptor desc : descriptions) {
				String fieldName = desc.getName();
				System.out.println("fieldNmae : " + fieldName);
				if(!fieldName.equals("class")) {
					Method getter = desc.getReadMethod();
					Object value = getter.invoke(obj, new Object[]{});
					vector.add(value);
				}
			}
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return vector;
	}
	*/
	public static Vector<?> convertToVector(Class<?> entity) {
		Vector<Object> vector = new Vector<Object>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(entity);
			PropertyDescriptor[] descriptions = beanInfo.getPropertyDescriptors();
			for(PropertyDescriptor desc : descriptions) {
				String fieldName = desc.getName();
				if(!fieldName.equals("class")) {
					Field field = entity.getDeclaredField(fieldName);
					Class<?> type = field.getType();
					vector.add(type);
				}
			}
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		return vector;
	}
	public static Vector<?> convertToVector(Object[] objs) {
		Vector<Object> vector = new Vector<Object>();
		for(Object obj : objs) {
			vector.add(obj);
		}
		return vector;
	}
}
